using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Peradigm.Framework.UI.Web;

using Liberty.Application.ServiceMemberManagement;
using Liberty.Application.CaseManagement;
using Liberty.Application.GrantManagement;
using Liberty.Domain.CaseManagement;
using Liberty.Domain.GrantManagement;
using Liberty.Domain.NoteManagement;

namespace Liberty.UI.GrantManagement
{

    public partial class CaseView : AbstractLibertyPage
    {      
        #region Constants     
        private static string refURL;

        private const string
                    ADD_GRANT_RELATIVE_URL = "~/GrantApplicationWizardView.aspx?grant=true",
                    GRANT_VIEW_RELATIVE_URL = "~/GrantManagement/GrantView.aspx";
        #endregion

        #region Event Handlers
        protected void Page_Load(object sender, EventArgs e)
        {
            refURL = this.Request.UrlReferrer.AbsoluteUri.ToString();
            List<Note> notes = this.NotesFrom(this.Case);
            List<Grant> grants = (List<Grant>)this.Case.Grants;

            this.ucCaseDetailView.Model = this.Case;
            this.ucCaseEditView.Model = this.Model;
            this.ucNoteListView.Model = notes;
            this.ucGrantSummaryView.Model = grants;
         
            this.ucNewNoteView.NoteAddedListeners += new NoteAdded(this.OnNoteAdded);
            this.ucGrantSummaryView.AddNewGrantListeners += new Liberty.Application.GrantManagement.AddNewGrantEventHandler(ucGrantSummaryView_AddNewGrantListeners);
            this.ucGrantSummaryView.GrantSelectedListeners += new GrantSelectedEventHandler( ucGrantSummaryView_GrantSelectedListeners );
        }
        protected void cmdEdit_Click(object sender, EventArgs e)
        {
            this.EditCase();
        }
        protected void cmdSave_Click( object sender, EventArgs e )
        {
            this.ucCaseEditView.SaveModel();
            #warning display readonly or if adding new grant move to next page
            Response.Redirect( "~/Default.aspx" );
        }
        protected void cmdCancel_Click( object sender, EventArgs e )
        {
            if( refURL != null || refURL != "" )
            {
                Response.Redirect( refURL );
            }
        }
        void ucGrantSummaryView_AddNewGrantListeners(Case aCase)
        {
            CaseVariable caseVariable = new CaseVariable();
            caseVariable.Case = aCase;
            Server.Transfer(ADD_GRANT_RELATIVE_URL);
        }
        private void ucGrantSummaryView_GrantSelectedListeners( Grant grant )
        {
            GrantVariable grantVariable = new GrantVariable();
            grantVariable.Grant = grant;
            Server.Transfer( GRANT_VIEW_RELATIVE_URL );
        }
        #endregion

        #region Methods
        protected override void UpdateView()
        {
            base.UpdateView();
            ( (Main)this.Master ).HideAddGrantAction();
        }
        #endregion

        #region Properties
        public override object Model
        {
            get
            {
                CaseVariable caseVariable = new CaseVariable();
                return caseVariable.Case;
            }
        }
        #endregion

        #region Private Methods
        private List<Note> NotesFrom( Case aCase )
        {
            ServiceMemberController controller = new ServiceMemberController();
            return controller.NotesFrom( aCase.ServiceMember );
        }

        protected void OnNoteAdded( Note note )
        {
            ServiceMemberController controller = new ServiceMemberController();
            controller.SaveNote( this.Case.ServiceMember, note );
            this.ucNoteListView.Model = this.NotesFrom( this.Case );
        }

        private void EditCase()
        {
            this.ucCaseDetailView.Visible = false;
            this.ucCaseEditView.Visible = true;
            this.cmdEdit.Visible = false;
            this.cmdSave.Visible = true;
            this.cmdCancel.Visible = true;
            this.ucNewNoteView.Visible = false;
            this.ucNoteListView.Visible = false;
            this.ucGrantSummaryView.Visible = false;
            this.GrantsHeader.Visible = false;
            this.GrantsBody.Visible = false;
            
        }
        #endregion

        #region Private Properties      
        private Case Case
        {
            get
            {
                return (Case)this.Model;
            }
        }
        #endregion

        #region Construction and Finalization 
        #endregion

        #region Data Elements
        #endregion


 
}
}

    

